
 // tendrils.cpp
 // draws "biological" forms
 #include <graphics.h>
 #include <stdlib.h>
 #include <time.h>
 #include <math.h>
 #include <conio.h>
 #include <iostream.h>

 #define GREEN  2
 #define RED  4
 #define LIGHTGREEN  10
 #define YELLO  14

 const int XO = 320 ;  // center of screen
 const int YO = 100 ;
 const float PI = 3.14159 ;  // pi
 const int NUME = 30 ; // numerator of probability
 const int DENOM = 100 ;
 const int NUMBER = 7 ;
 const float RAD = 3.0 ;
 const float DELTHETA = 0.1 ;
 const int SEGS = 60 ;
 const REDUX = 3 ;
 const int MIN = 1 ;

 class cluster
  {
    public :
       void display (int size,int x,int y) ;
   } ;

 class tendril
  {
    public :
       void display (int size,float theta,int x,int y) ;
   } ;

 void cluster::display (int size,int x,int y)
  {
    if ( kbhit () )
      exit (0) ;

    for ( int i = 0 ; i < NUMBER ; i++ )
     {
       float theta = i * 2 * PI / NUMBER ;
       moveto (x,y) ;
       tendril t ;
       t.display (size,theta,x,y) ;
      }
   }

 void tendril::display (int size,float theta,int x,int y)
  {
    for ( int j = 0 ; j < size ; j++ )
     {
       int randy = rand () % DENOM ;
       int chng = ( randy < NUME ) ? -1 : 1 ;
       theta = theta + chng * DELTHETA ;
       x = x + RAD * sin (theta) ;
       y = y + RAD * cos (theta) ;
       if ( size < 4 )
         setcolor (RED) ;
       else
         if ( size < 13 )
           setcolor (GREEN) ;
         else
           if ( size < 40 )
             setcolor (LIGHTGREEN) ;
           else
             setcolor (YELLOW) ;
       lineto (x,y) ;
      }
    if ( size > MIN )
     {
       cluster c ;
       int newsize = size / REDUX ;
       c.display (newsize,x,y) ;
      }
   }

 void main ()
  {
    int gd = CGA , gm = CGAHI ;
/*
    if ( ! _setvideomode (_VRES16COLOR) )
     {
       cout << "Can't set graphics mode" ;
       exit (-1) ;
      }
*/

    initgraph (&gd,&gm,"C:\\bc\\") ;

    srand ((unsigned) time (NULL)) ;
    int x = XO , y = YO ;
    int size = SEGS ;
    cluster c ;
    c.display (size,x,y) ;
    getch () ;
//    _setvideomode (_DEFAULTMODE) ;

    closegraph () ;
   }
